/**
 * Malsha 22/07/2014
 */

Loging = new Object();

Ext.onReady(function() {
	var window = null;
	var login_form = Ext.create('Ext.form.Panel', {
		url : 'userlogin.action',
		frame : true,
		renderTo : Ext.get('logindiv'),
		bodyStyle : "background-image:url('images/investment.jpg')",
		header : false,
		bodyBorder : false,
		border : false,
		layout : 'absolute',
		width : 770,
		height : 262,
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'User Name',
			name : 'user',
			labelAlign : 'top',
			allowBlank : false,
			x : 20,
			y : 170
		}, {
			xtype : 'textfield',
			fieldLabel : 'Password',
			name : 'password',
			allowBlank : false,
			labelAlign : 'top',
			inputType : 'password',
			x : 190,
			y : 170,
			listeners : {
				specialkey : function(field, e) {
					if (e.getKey() == e.ENTER) {
						var myMask = new Ext.LoadMask(login_form, {
							msg : "Login..."
						});
						myMask.show();
						login_form.getForm().submit({
							success : function(form, action) {
								App.Core.getMainView(action.result.user);
								window.hide();
								myMask.hide();
							},
							failure : function(form, action) {
								if (action.result.resultMsg == "2") {
									Ext.Msg.alert('Warning', 'User unavailable');
								} else {
									Ext.Msg.alert('Warning', 'Unable to login. Please try again.');
								}
								myMask.hide();
							}
						});
					}
				}
			}
		}, {
			xtype : 'button',
			name : 'reset',
			text : 'Reset',
			width : 70,
			x : 190,
			y : 220,
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			xtype : 'button',
			name : 'login',
			text : 'Log In',
			width : 70,
			x : 270,
			y : 220,
			handler : function() {
				var myMask = new Ext.LoadMask(login_form, {
					msg : "Login..."
				});
				myMask.show();
				login_form.getForm().submit({
					success : function(form, action) {
						App.Core.getMainView(action.result.user);
						window.hide();
						myMask.hide();
					},
					failure : function(form, action) {
						if (action.result.resultMsg == "2") {
							Ext.Msg.alert('Warning', 'User unavailable');
						} else {
							Ext.Msg.alert('Warning', 'Unable to login. Please try again.');
						}
						myMask.hide();
					}
				});
			}
		} ]
	});

	window = Ext.create('Ext.window.Window', {
		layout : 'fit',
		frame : true,
		header : false,
		bodyBorder : false,
		border : false,
		resizable : false,
		items : [ login_form ]
	}).show();
});